﻿using Feature.YiGuo.Framework.Utility.Network;
using Feature.YiGuo.Framework.Utility.Utils;
using Newtonsoft.Json;
using System;
using System.IO;

namespace CheckTaskUpdate
{
    class Program
    {
        static void Main(string[] args)
        {
            string baseUrl =
                "http://pmsapi.yiguo.com/api/UserStatistic/Info?statisticType=W&teamId=39&startDate={1}&endDate={2}&account={0}";

            var data = File.ReadAllLines(Path.Combine(System.AppDomain.CurrentDomain.BaseDirectory, "data/user.txt"));
            foreach (var s in data)
            {
                if (string.IsNullOrEmpty(s))
                {
                    continue;
                }
                var user = s.Split('|');
                var day = (int)DateTime.Now.DayOfWeek;
                var url = string.Format(baseUrl, user[0], DateTime.Now.AddDays(-day + 1).ToShortDateString(), DateTime.Now.ToShortDateString());

                var result = ServiceAgent.Get(url);

                var taskInfo = JsonConvert.DeserializeObject<Rootobject>(result);

                Console.WriteLine("{2}打卡工时{0},任务工时{1}", taskInfo.data.workHours, taskInfo.data.consumed, s);

                if (taskInfo.data.workHours - taskInfo.data.consumed > 8)
                {
                    var msg = string.Format("打卡工时{0},任务工时{1}", taskInfo.data.workHours, taskInfo.data.consumed);
                    SendDingMessage(msg, user[1]);
                    Console.WriteLine(msg);
                }
            }
            Console.WriteLine("执行完成");
            Console.ReadLine();
        }

        static void SendDingMessage(string msg, string mobile)
        {
            var m = new DingMessage
            {
                msgtype = "text",
                text = new Text()
                {
                    content = msg
                },
                at = new At()
                {
                    atMobiles = new[] { mobile },
                    isAtAll = false
                }
            };
            string url = ConfigHelper.GetValue("DingUrl");

            ServiceAgent.SendRequest(url, m);
        }
    }



    public class DingMessage
    {
        public string msgtype { get; set; }
        public Text text { get; set; }
        public At at { get; set; }
    }

    public class Text
    {
        public string content { get; set; }
    }

    public class At
    {
        public string[] atMobiles { get; set; }
        public bool isAtAll { get; set; }
    }



    public class Rootobject
    {
        public object message { get; set; }
        public int code { get; set; }
        public Data data { get; set; }
    }

    public class Data
    {
        public int assignedTaskCount { get; set; }
        public int finishedTaskCount { get; set; }
        public float consumed { get; set; }
        public int bugCount { get; set; }
        public string saturatedRate { get; set; }
        public string bugRate { get; set; }
        public float workHours { get; set; }
    }

}
